/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_frcmd_send_data.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/03 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "../comm/apmtest_cunit.h"
#include "frcomm.h"
#include "frmem.h"
#include "frcmd_server.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}


void case1(void)
{
    int i_ret = 0;
    int sockfd = 5;
    char *p_data = NULL;
    unsigned int ui_data_len = 5;

    p_data = frmalloc(ui_data_len);
    if(p_data == NULL)
        return;

    snprintf(p_data,ui_data_len,"%s","test");

    i_ret = frcmd_send_data(sockfd,p_data,ui_data_len);

    CU_ASSERT_EQUAL(i_ret,0);

    frfree(p_data);

}

void case2(void)
{
    int i_ret = 0;
    int sockfd = 5;
    char *p_data = NULL;
    unsigned int ui_data_len = 10;

    p_data = frmalloc(ui_data_len);
    if(p_data == NULL)
        return;

    snprintf(p_data,ui_data_len,"%s","test test");

    p_data[4] = '\0';

    i_ret = frcmd_send_data(sockfd,p_data,ui_data_len);

    CU_ASSERT_EQUAL(i_ret,0);

    frfree(p_data);

}

void case3(void)
{
    int i_ret = 0;
    int sockfd = 5;
    char *p_data = NULL;
    unsigned int ui_data_len = 1;

    p_data = frmalloc(ui_data_len);
    if(p_data == NULL)
        return;

    snprintf(p_data,ui_data_len,"%s","\0");

    i_ret = frcmd_send_data(sockfd,p_data,ui_data_len);

    CU_ASSERT_EQUAL(i_ret,0);

    frfree(p_data);

}


void case4(void)
{
    int i_ret = 0;
    int sockfd = 5;
    char *p_data = NULL;
    unsigned int ui_data_len = 0;
    FILE *fp_stdin = NULL;

    fp_stdin = fopen("send_data_case1.txt","r");
    if(fp_stdin == NULL)
        return;

    fseek(fp_stdin,0,SEEK_SET);
    fseek(fp_stdin,0,SEEK_END);

    ui_data_len = ftell(fp_stdin);
    ui_data_len++;

    rewind(fp_stdin);

    p_data = frmalloc(ui_data_len);
    if(p_data == NULL)
    {
        return;
        fclose(fp_stdin);
    }

    fread(p_data,1,ui_data_len,fp_stdin);

    i_ret = frcmd_send_data(sockfd,p_data,ui_data_len);

    CU_ASSERT_EQUAL(i_ret,0);

    frfree(p_data);
    fclose(fp_stdin);

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t_frcmd_send_data 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

    CU_add_test(pSuite, "case2", case2);

    CU_add_test(pSuite, "case3", case3);

    CU_add_test(pSuite, "case4", case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}

